package com.suncky.frame.http.converter;

import com.google.gson.reflect.TypeToken;
import com.suncky.frame.utils.GsonUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

public final class GsonConverterFactory extends Converter.Factory {

    public static GsonConverterFactory create() {
        return new GsonConverterFactory();
    }

    private GsonConverterFactory() {
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        return new GsonResponseBodyConverter<>(GsonUtils.getGson(), GsonUtils.getGson().getAdapter(TypeToken.get(type)));
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        return new GsonRequestBodyConverter<>(GsonUtils.getGson(), GsonUtils.getGson().getAdapter(TypeToken.get(type)));
    }

}
